/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.net.connections {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import ua.kiev.gcore.net.ServerProxy;
	import ua.kiev.gcore.net.command.Command;
	import ua.kiev.gcore.net.command.IProtocolParser;
	import ua.kiev.gcore.net.connections.event.CommunicateEvent;
	
	
	public class HTTPConnection extends Connection {
		
		private var loader:URLLoader;
		private var apiLoader:URLLoader;
		private var noCashe:int = 0;
		private var request:URLRequest;
		private var apiRequest:URLRequest;
		private var buffer:ByteArray = new ByteArray();
		
		private var isHttpSend:Boolean = false;
		
		private var sendCommandStack:Array;
		
		
		private var requestVars:URLVariables;
		
		
		public function HTTPConnection(id:int, ca:ConnectionAttributes, parser:IProtocolParser) {
			super(id, ca, parser);
			
			sendCommandStack = new Array();
		}
		
		/**
		 * Connect to server
		 */
		override public function init():void {
			
			requestVars = new URLVariables();
			
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			request = new URLRequest();
			
			apiRequest = new URLRequest();
			
			// run send/receive timers
			var timerS:Timer = new Timer(50);
			timerS.addEventListener(TimerEvent.TIMER, oneTickSendHandler);
			timerS.start();
			
			dispatchEvent(new CommunicateEvent(CommunicateEvent.CONNECT));
		}
		
		/**
		 * timers handlers
		 */
		private function oneTickSendHandler(event:TimerEvent):void {
			if (!isHttpSend && sendCommandStack.length > 0) {
				send(sendCommandStack.shift());
			}
		}
		
		override public function sendCommand(command:Command):void {
			if (!isHttpSend && sendCommandStack.length == 0) {
				send(command);
			} else {
				sendCommandStack.push(command);
			}
		}
		
		
		
		/**
		 * send command
		 * @param command command string
		 */
		public function send(command:Command):void {
			   if(command.type == Command.POST){
				   request = new URLRequest(command.adress);
				   
				   request.data =  command.data as URLVariables;
				   request.method = URLRequestMethod.POST;
				   try {
						loader.load(request);
						isHttpSend = true;
				   } catch (error:Error) {
					   trace("connection error");
				   }
			   }else{
				   trace("invalid type http command");
			   }
            
  		}
  
 		/**
		 * Came the answer
		 */
		private function completeHandler(event:Event):void {
			addReceiveCommand(loader.data as String);
			isHttpSend = false;
		}
		
		/**
		 * add command in stack
		 */
		private function addReceiveCommand(command:String):void {
			if(listener){
				listener.parse(command);
			}
		}
		
		
		/**
		 * No any connection
		 */
		private function ioErrorHandler(event:IOErrorEvent):void {
		}
	}
}